import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

var h5NavHeight = 0;
/*#ifdef APP-PLUS*/
h5NavHeight = 44;
/*#endif*/
/*#ifdef H5*/
// const h5NavHeight = 0;
/*#endif*/

const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync('token') || "",
		info: uni.getStorageSync('info') || {
			head_portrait: "",
			nickname: "",
			address: "",
			is_realname: 0,
			show_phone: "",
			phone: "",
			balance: 0.00,
			invitation_code: null
		},
		loadText: {
			loadmore: '点击或上拉加载更多',
			loading: '努力加载中',
			nomore: '没有更多了'
		},
		h5NavHeight: h5NavHeight,
		market_open: false,
		phb_open: 2,
		payment_src: uni.getStorageSync('payment_src') || "",
		// 全局数据处理
		webSocket: null,
		klinetypeCurrency: localStorage.getItem("klinetypeCurrency") || "BTC/USDT", //选中的
		// klineLogo: require("@/assets/img/deal/btc.png"),/*  */
		klineLogo:'',
		currencyId: localStorage.getItem("currencyId") || "1", //币种id
		klineDateTime: "1min",
		label: "4", //3币币 4合约 5期权  默认合约
		chart: {}, //k线数据
		realData: {}, //1天的实时数据
		ask_bid: {}, //订单表
		arrCurrencyData: {}, //所有的币种
		// 币种信息及配置
		currency_data:[]
	},
	mutations: {
		changeLogin(state, payload) {
			state.info.head_portrait = payload.head_portrait;
			state.info.nickname = payload.nickname;
			state.info.address = payload.address;
			state.info.is_realname = payload.is_realname;
			state.info.show_phone = payload.show_phone;
			state.info.phone = payload.phone;
			state.info.balance = payload.balance;
			state.info.invitation_code = payload.invitation_code;
			uni.setStorageSync('info', payload);
		},
		changeToken(state, payload) {
			state.token = payload;
			uni.setStorageSync('token', payload);
		},
		change_payment_src(state, payload) {
			state.payment_src = payload;
			uni.setStorageSync('payment_src', payload);
		},
		outLogin(state, payload) {
			state.token = null;
			state.info = {
				head_portrait: "",
				nickname: "",
				address: "",
				is_realname: 0,
				show_phone: "",
				balance: 0.00
			};
			uni.removeStorageSync("info");
			uni.removeStorageSync("token");
		},
		changeSetup(state, payload) {
			state.market_open = payload.market_open;
			state.phb_open = payload.phb_open;
		},
		// 链接websocket
		initWebsocket(state) {
			state.webSocket = new WebSocket(
				// 此处填写你要连接的ws地址
				"wss://coinspace.top/socket.io/?EIO=3&transport=websocket"
			);
			//建立连接
			state.webSocket.onopen = function() {
				/*
				 * 连接成功
				 * */
				console.log("通讯开始");
				// 发送心跳防止ws协议自动断联
				setInterval(() => {
					state.webSocket.send("1");
				}, 1000 * 60);
			};
			//接收服务端消息
			state.webSocket.onmessage = function(e) {
				// console.log(e.data);
				try {
					// 监听到服务器消息
					let data = JSON.parse(e.data.slice(2));
					let dataType = data[0];
					// console.log(data);
					/*
					 * 收到消息时回调函数
					 * */
					// console.log('收到的数据：', e.data)
					// 如果数据对象为字符串，可转换为对象格式

					// K线图
					if (
						data[1].symbol == state.klinetypeCurrency &&
						dataType == "kline" &&
						data[1].period == state.klineDateTime
					) {
						data[1].timestamp = data[1].time;
						// console.log(data[1], '-----------------------');
						// const newArr = [...state.chart];
						// const existingDataIndex = newArr.findIndex((item) => item.currency_id === data[1].currency_id);
						// if (existingDataIndex !== -1) {
						//   newArr[existingDataIndex] = data[1];
						// } else {
						//   newArr.push(data[1]);
						// }
						// console.log(data[1]);
						state.chart = data[1];
						// console.log(state.chart)
					}
					// k线数据详情
					//详情
					if (
						(dataType == "daymarket" || dataType == "kline") &&
						data[1].symbol == state.klinetypeCurrency &&
						data[1].period == "1day"
					) {
						data[1].quota = data[1].volume * data[1].close;

						data[1].now_price = data[1].close;
						data[1].isType = data[1].change.slice(0, 1);
						data[1].high = data[1].high;
						data[1].low = data[1].low;
						data[1].volume = data[1].volume;
						// console.log(data[1]);
						state.realData = data[1];
						// uni.$emit("daymarket", data[1]);
					}
					//订单表
					if (
						dataType == "market_depth" &&
						data[1].symbol == state.klinetypeCurrency
					) {
						// console.log(data[1]);
						state.ask_bid = data[1];
						// uni.$emit("market_depth", data[1]);
					}
					//all
					if ((dataType == "daymarket" || dataType == "kline") && data[1].period == "1day") {
						// console.log(data[1]);
						data[1].isType = data[1].change.slice(0, 1);
						state.arrCurrencyData = data[1];
						// uni.$emit("daymarketAll", data[1]);
					}
				} catch (err) {
					console.log(err.message, "发生错误");
				}
			};
			state.webSocket.onerror = function() {
				/*
				 * 通讯异常
				 * */
				console.log("通讯异常");
			};
			state.webSocket.close = function() {
				/*
				 * 关闭连接时回调函数
				 * */
				console.log("连接已断开");
				commit("initWebsocket");
			};
		},
		//币种类型
		setKlinetypeCurrency(state, value) {
			state.klinetypeCurrency = value;
			localStorage.setItem("klinetypeCurrency", value);
		},
		//币种ID
		setCurrencyId(state, value) {
			state.currencyId = value;
			localStorage.setItem("currencyId", value);
		},
		//币种Logo
		setCurrencyLogo(state, value) {
			state.klineLogo = value;
			localStorage.setItem("klineLogo", value);
		},
		setLable(state, value) {
			state.label = value;
			localStorage.setItem("label", value);
		},
		// 设置k线数据
		setChart(state, value) {
			// console.log(value);
			state.chart = value;
			// console.log(state.chart);
		},
		// 设置k线数据的时间
		setKlineDateTime(state, value) {
			state.klineDateTime = value;
		},
		// 保存币种信息及配置列表
		setCurrency_data(state, value) {
			// localStorage.setItem("currency_data", JSON.stringify(value));
			state.currency_data = value
		},
	},
	actions: {

	}
})

export default store